package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqEmailCode;
import com.lanlian.commonlib.bean.ReqModifyEmail;
import com.lanlian.commonlib.bean.db.DBUser;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.TimeCount;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.utillib.CommonUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 13:27
 *     desc   : 修改邮箱下一步
 *     version: 1.0
 * </pre>
 */
public class UpdateEmailNextActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtEmail;
    private TextView mTvCode;
    private EditText mEtCode;
    private TextView mTvOk;

    private String mVerifyCode;
    private String mValKey;
    private TimeCount mTimeCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_email_next, true);
        initData();
        initView();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("修改邮箱");
        mEtEmail = findViewById(R.id.et_email);
        mTvCode = findViewById(R.id.tv_code);
        mEtCode = findViewById(R.id.et_code);
        mTvOk = findViewById(R.id.tv_ok);
        mTimeCount = new TimeCount(this, 60000, 1000, mTvCode);
    }

    @Override
    protected void initData() {
        super.initData();
        mVerifyCode = getIntent().getStringExtra("verifyCode");
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvCode.setOnClickListener(this);
        mTvOk.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvOk);
        worksSizeCheckUtil.addAllEditText(mEtEmail, mEtCode);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvOk.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvOk.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_ok:
                ReqModifyEmail reqModifyEmail = new ReqModifyEmail();
                reqModifyEmail.setCode(mEtCode.getText().toString());
                reqModifyEmail.setValKey(mValKey);
                reqModifyEmail.setVerifyCode(mVerifyCode);
                reqModifyEmail.setMail(mEtEmail.getText().toString());
                showLoadingDialog();
                VolleyRequestUtils.getInstance().modifyEmail(this.toString(), reqModifyEmail, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        String email = mEtEmail.getText().toString();
                        DBUser user = mDBManager.getUser();
                        user.setMail(email.substring(0, 1) + "****" + email.substring(email.indexOf("@") - 2, email.length()));
                        mDBManager.updateUser(user);
                        dismissLoadingDialog();
                        showToast("修改成功");
                        finish();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            case R.id.tv_code:
                if (TextUtils.isEmpty(mEtEmail.getText().toString())) {
                    showToast("请输入邮箱");
                    return;
                }
                if (!CommonUtils.isEmail(mEtEmail.getText().toString().trim())) {
                    showToast("请输入正确的邮箱格式");
                    return;
                }
                //获取验证码
                ReqEmailCode reqEmailCode = new ReqEmailCode();
                reqEmailCode.setEmail(mEtEmail.getText().toString());
                mTvCode.setClickable(false);
                VolleyRequestUtils.getInstance().sendEmailCode(this.toString(), reqEmailCode, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mTimeCount.startTime();
                        showToast("验证码已发送到你的新邮箱");
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mTvCode.setClickable(true);
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mTimeCount.cancel();
        mTimeCount = null;
        super.onDestroy();
    }


}
